use cxx::UniquePtr;
use once_cell::sync::OnceCell;
use std::{
    ffi::{c_char, CStr, CString},
    fmt::format, time::Duration,
};

use server::Server;

mod server;

#[cxx::bridge(namespace = "server")]
mod ffi {

    extern "Rust" {

        pub fn init();
        pub unsafe fn listen(path: *const c_char, port: i32);
    }
}

static mut SERVER: OnceCell<Server> = OnceCell::new();

pub fn init() {
    let server = Server::new();
    unsafe { let _ = SERVER.set(server); };
}
pub unsafe fn listen(path: *const c_char, port: i32) {
    if let Some(server) = SERVER.get_mut() {
        server.listen(path, port);
    }
}

#[test]
fn start() {
    let mut server = Server::new();

    unsafe {
        server.listen(
            CString::new("D:/Projects/rust/static-server/www")
                .unwrap()
                .as_ptr(),
            8080,
        );
       
    };
}
